package com.yao.ro.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yao.common.enums.business.ro.FinishStatusEnum;
import com.yao.common.exception.BusinessException;
import com.yao.ro.entity.dto.EquipmentDto;
import com.yao.ro.entity.dto.EquipmentListDto;
import com.yao.ro.entity.dto.ExchangeDto;
import com.yao.ro.entity.dto.GoodsDto;
import com.yao.ro.entity.vo.EquipmentVo;
import com.yao.ro.mapper.EquipmentMapper;
import com.yao.ro.service.EquipmentListService;
import com.yao.ro.service.EquipmentService;
import com.yao.ro.service.ExchangeService;
import com.yao.ro.service.GoodsService;
import com.yao.ro.util.PriceUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
* 装备制作表 接口实现
*
* @author autoCode
* @version 1.0.0-SNAPSHOT
* @date 2020-04-22
*/
@Service
public class EquipmentServiceImpl extends ServiceImpl<EquipmentMapper, EquipmentDto> implements EquipmentService{

    @Autowired
    private EquipmentMapper mapper;

    @Autowired
    private EquipmentListService equipmentListService;

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private ExchangeService exchangeService;

    @Override
    @Transactional(isolation = Isolation.REPEATABLE_READ, rollbackFor = BusinessException.class)
    public void add(EquipmentDto dto) {
        // 商品信息
        GoodsDto goods = Optional.ofNullable(goodsService.getById(dto.getGoodsId())).orElseThrow(() -> new BusinessException("查询不到商品{}信息", dto.getGoodsId()));

        // 交易所信息
        ExchangeDto exchange = exchangeService.getByGoodsId(dto.getGoodsId());

        dto.setEquipName(goods.getName());

        if(Objects.nonNull(exchange)){
            dto.setPrice(exchange.getPrice())
                    .setHistoryMaxPrice(exchange.getHistoryMaxPrice())
                    .setHistoryMinPrice(exchange.getHistoryMinPrice())
                    .setFinishStatus(FinishStatusEnum.UNFINISHED.getCode());
        }

        if (!this.save(dto)) {
            throw new BusinessException("新增装备失败.");
        }
    }

    @Override
    public IPage<EquipmentVo> page(IPage<EquipmentDto> page, EquipmentDto dto) {


        IPage<EquipmentVo> result = mapper.page(page, dto);

        result.getRecords().forEach(e -> {

            // 价格取4位
            e.setCost(PriceUtils.formatter(e.getCost()));
            e.setPrice(PriceUtils.formatter(e.getPrice()));
            e.setHistoryMaxPrice(PriceUtils.formatter(e.getHistoryMaxPrice()));
            e.setHistoryMinPrice(PriceUtils.formatter(e.getHistoryMinPrice()));

            LambdaQueryWrapper<EquipmentListDto> listQuery = Wrappers.lambdaQuery();
            listQuery.eq(EquipmentListDto::getEqmId, e.getId());

            List<EquipmentListDto> list = equipmentListService.list(listQuery);

            //需要材料总数
            e.setNeeds(list.stream().count());
            //剩余材料数量
            e.setReminding(list.stream().filter(l -> FinishStatusEnum.UNFINISHED.getCode() == l.getCollectStatus().intValue()).count());
        });


        return result;
    }


}